package org.example.partnerbacked.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.partnerbacked.common.BaseResponse;
import org.example.partnerbacked.common.ErrorCode;
import org.example.partnerbacked.common.ResultUtils;
import org.example.partnerbacked.config.ConfigSession;
import org.example.partnerbacked.exception.BusinessException;
import org.example.partnerbacked.model.Team;
import org.example.partnerbacked.model.User;
import org.example.partnerbacked.model.requste.TeamAddRequest;
import org.example.partnerbacked.service.TeamService;
import org.example.partnerbacked.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/team")
public class TeamController {
    @Resource
    private TeamService teamService;
    @Resource
    private UserService userService;
    @PostMapping("add")
    //加人队伍
    public BaseResponse<Long> addTeam(TeamAddRequest teamAddRequest, HttpServletRequest request) throws BusinessException {
        User user= (User) request.getAttribute(ConfigSession.USER_LOGIN_STATE);
        if(user==null) throw new BusinessException(ErrorCode.PARAMS_ERROR,"未登录");
        Team team=new Team();
        //将teamAddRequest中的值赋给team
        BeanUtils.copyProperties(teamAddRequest, team);
        long  result = teamService.addTeam(team, user);
        return ResultUtils.success(result);

    }
    @PostMapping("join")
    //加人队伍
    public BaseResponse<Boolean> joinTeam(@RequestParam(required = false)String idverified, Long teamId, HttpServletRequest request) throws BusinessException {
        User user= (User) request.getAttribute(ConfigSession.USER_LOGIN_STATE);
        if(user==null) throw new BusinessException(ErrorCode.PARAMS_ERROR,"未登录");
        boolean result = teamService.joinTeam(idverified,teamId, user);
        return ResultUtils.success(result);

    }

    @PostMapping("delete")
    public boolean deleteTeam(Long teamId, HttpServletRequest request) {
        User user= (User) request.getAttribute(ConfigSession.USER_LOGIN_STATE);
        return teamService.deleteTeam(teamId,user);
    }
    //退出队伍
    @PostMapping("quit")
    public boolean quitTeam(Long teamId, HttpServletRequest request) {
        User user= (User) request.getAttribute(ConfigSession.USER_LOGIN_STATE);
        return teamService.quitTeam(teamId,user);
    }
    @PostMapping("update")
    public boolean updateTeam(Team team, HttpServletRequest request) {
        User user= (User) request.getAttribute(ConfigSession.USER_LOGIN_STATE);
        return teamService.updated(team,user);
    }
}
